** Note: Run the code for Table 3 code AFTER running the code for Table 2: 
* 2 - analysis - Baseline table - Metropolitan Areas Sample part 1.do 
* 2 - analysis - Baseline table - Metropolitan Areas Sample part 2.do

** Preamble **
 
local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/tables"


* Note: local directory is the temporary folder 
cd "`path_output'"

					** Adjusting data **

drop if  metrop3==3 & wave==3
drop if  metrop2==3 & wave==2
drop if  metrop1==3 & wave==1


** Lula won the debate **
cap drop Lulawon*
gen Lulawon3=0
replace Lulawon3=1 if (W3_q4==1)  & wave==3
gen Lulawon2=0
replace Lulawon2=1 if (W2_q12==1) & wave==2 
gen Lulawon=0
replace Lulawon=1  if Lulawon2==1
replace Lulawon=1  if Lulawon3==1
*replace Lulawon=. if watch==0
replace Lulawon=.  if wave==1
*replace Lulawon=. if W3_q4==0
drop Lulawon2 Lulawon3

** Individual demographics **

* gen wave2=(wave==3)

replace SEXO=0 if SEXO==2

replace IDADE=16.5      if IDADE==1
replace IDADE=(18+25)/2 if IDADE==2
replace IDADE=(26+40)/2 if IDADE==3
replace IDADE=(41+90)/2 if IDADE==3

replace escola=0    if escola==1
replace escola=4.5  if escola==2
replace escola=9    if escola==3
replace escola=10.5 if escola==4
replace escola=12   if escola==5
replace escola=14   if escola==6
replace escola=16   if escola==7
replace escola=18   if escola==8

destring IDADE, replace
destring escola, replace


***descriptive statistics for the survey data
drop if escola==.

drop if titulo==0

											** Table: Balance check, survey data **

label var vota_lula_2oturno   "Vote for Lula"
label var vota_collor_2oturno "Vote for Collor"
label var Lulawon             "Lula won debate"
label var watch               "Watched debate"

label var IDADE  "Age"
label var SEXO   "Male"
label var escola "Years of education"

local electoral    "vota_lula_2oturno vota_collor_2oturno Lulawon watch"
local demographics "IDADE SEXO escola"

foreach v of varlist * {

	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
	
}

** Forcing sample to be the same **
keep if sample_MSAs==1

											** Panel A **


balancetable (mean if MA==0) (mean if MA==1) (diff MA) `electoral' [aw=peso] using Table_balance_MSAs_panel_A.tex, replace /// 
			  wide(mean1 sd1 mean2 sd2 pval3) vce(cluster estado) 															  /// 
			  pvalues varlabel nonumbers noobservations nopar          														  ///
			  ctitles("Mean" "Std. dev." "Mean" "Std. dev." "p-value") 														  ///
			  groups("Treatment" "Control", pattern(1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}))					  		
			  

											** Panel B **

balancetable (mean if MA==0) (mean if MA==1) (diff MA) `demographics' [aw=peso] using Table_balance_MSAs_panel_B.tex, replace /// 
			  wide(mean1 sd1 mean2 sd2 pval3) vce(cluster estado) 															  /// 
			  pvalues varlabel nonumbers  nopar          														  ///
			  ctitles("Mean" "Std. dev." "Mean" "Std. dev." "p-value") 														  ///
			  groups("Treatment" "Control", pattern(1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}))					  ///		
			  
			 
include "https://raw.githubusercontent.com/steveofconnell/PanelCombine/master/PanelCombine.do"

panelcombine, use(Table_balance_MSAs_panel_A.tex Table_balance_MSAs_panel_B.tex) 	   				///
paneltitles("Voting intentions and debate" "Individual demographics") columncount(6)    			/// 
save(Table_Descriptive_MetropolitanAreasSample.tex) cleanup


* di 8145 + 118 
* = 8263 

